﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web.UI.WebControls;

using SLIITPro.Data.Entity;
using SLIITPro.Data.Extension;

namespace SLIITPro.SysUsers
{
    public partial class CommentGrid : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btnSave.Click += new EventHandler(btnSave_Click);
            btnDelete.Click += new EventHandler(btnDelete_Click);
            if (!Page.IsPostBack)
                LoadData();
        }

        public List<Data.Entity.Comments> Comment { get; set; }

        protected string GetLinkText(bool id)
        {
            return id ? "Reject" : "Approve";
        }

        protected Color GetLinkColor(bool id)
        {
            return id ? Color.Red : Color.Green;
        }

        void btnDelete_Click(object sender, EventArgs e)
        {
            dvComment.DeleteItem();
            this.dvComment.Visible = false;
            this.mdlPopup.Hide();
            LoadData();
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            dvComment.UpdateItem(false);
            this.dvComment.Visible = false;
            this.mdlPopup.Hide();
            LoadData();
        }

        protected void grdComment_SelectedIndexChanged(object sender, EventArgs e)
        {
            dvComment.Visible = true;
            dvComment.DataBind();
            updPnlComment.Update();
            mdlPopup.Show();
        }

        protected void dsCommentUpdate_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {
            e.Command.Parameters[0].Value = Convert.ToString(grdComment.DataKeys[grdComment.SelectedIndex].Value);
        }



        protected void grdComment_IndexChanging(object sender, GridViewPageEventArgs e)
        {
            LoadData();
            grdComment.PageIndex = e.NewPageIndex;
            grdComment.DataBind();
            upCommentEdit.Update();
        }

        private CommentExtension _commentExtension;
        protected void HyperLinkApprove_Click(object sender, EventArgs e)
        {
            var lnkbtn = (LinkButton)sender;
            var id = lnkbtn.CommandArgument;
            _commentExtension = new CommentExtension();
            var selectComment = _commentExtension.SelectComment().Where(c => c.Id == id).FirstOrDefault();
            if (selectComment != null) _commentExtension.ModerateComment(!selectComment.IsApproved, id);
            LoadData();
            grdComment.DataBind();

        }
        private void LoadData()
        {
            _commentExtension = new CommentExtension();
            if (Page.User.IsInRole("administrator") || Page.User.IsInRole("content"))
                Comment = _commentExtension.SelectComment().OrderByDescending(c => c.DateCreated).ToList();

            else
            {
                var user = new Users(new UserExtension().SelectUser("Email", (string)Page.User.Identity.Name).Rows[0]);
                Comment = _commentExtension.SelectCommentByUser(user.UserId).OrderByDescending(c => c.DateCreated).ToList();
            }

            grdComment.DataSource = Comment;
            grdComment.DataBind();
            upCommentEdit.Update();
        }
        protected void btnMultiDelete_Click(object sender, EventArgs e)
        {
            _commentExtension = new CommentExtension();
            try
            {
                foreach (GridViewRow row in grdComment.Rows)
                {
                    var chkbx = (CheckBox)row.FindControl("chkDelete");
                    if (chkbx != null && chkbx.Checked)
                    {
                        _commentExtension.DeleteCommentByCommentId((string)grdComment.DataKeys[row.RowIndex].Value);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            LoadData();
        }

        protected void btnSelectAll_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in grdComment.Rows)
            {
                var chkbx = (CheckBox)row.FindControl("chkDelete");
                if (chkbx == null) continue;
                chkbx.Checked = !chkbx.Checked;
                btnSelectAll.Text = chkbx.Checked ? "Deselect All" : "Select All";
            }
        }
    }
}